<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="用户名" prop="userName">
        <el-input v-model="queryParams.userName" placeholder="请输入用户名" clearable size="small"
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="用户手机号" prop="userPhone">
        <el-input v-model="queryParams.userPhone" placeholder="请输入用户手机号" clearable size="small"
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="TUserInfoList">
      <el-table-column label="用户ID" show-overflow-tooltip align="center" prop="userId"/>
      <el-table-column label="用户名" align="center" prop="userName"/>
      <el-table-column label="用户手机号" align="center" prop="userPhone"/>
      <el-table-column label="直属上级" align="center" prop="superiorUserName"/>
      <el-table-column label="总交易金额" align="center" prop="totalTransAmount"/>
      <el-table-column label="总交易笔数" align="center" prop="myTransNum"/>
      <el-table-column label="创建时间" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200px;">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleDetail(scope.row)"
                     v-hasPermi="['pos:TUserInfo:edit']">详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-tabs v-model="activeName" @tab-click="handleTabClick" type="border-card">
        <el-tab-pane name="transfer" label="划拨记录">
          <el-table :data="tableData" style="width: 100%">
            <el-table-column prop="outUserName" label="划拨用户"/>
            <el-table-column prop="inUserName" label="划拨接收用户"/>
            <el-table-column prop="snCode" label="终端SN号"/>
            <el-table-column prop="transOutTime" label="划拨时间"/>
            <el-table-column prop="transInTime" label="划拨接收时间"/>
            <el-table-column prop="remark" label="备注"/>
            <el-table-column prop="date" label="操作人"/>
          </el-table>
        </el-tab-pane>
        <el-tab-pane name="transaction" label="交易金兑换记录">
          <el-table :data="tableData" style="width: 100%">
            <el-table-column prop="belongingUser" label="所属用户"/>
            <el-table-column prop="exchangeUser" label="兑换用户"/>
            <el-table-column prop="serialId" label="交易金序列号"/>
            <el-table-column prop="exchangeTime" label="兑换时间"/>
            <el-table-column prop="orderNo" label="兑换订单编号"/>
          </el-table>
        </el-tab-pane>
        <el-tab-pane name="reward" label="奖励金兑换记录">
          <el-table :data="tableData" style="width: 100%">
            <el-table-column prop="belongingUser" label="所属用户"/>
            <el-table-column prop="exchangeUser" label="兑换用户"/>
            <el-table-column prop="serialId" label="奖励金序列号"/>
            <el-table-column prop="exchangeTime" label="兑换时间"/>
            <el-table-column prop="orderNo" label="兑换订单编号"/>
          </el-table>
        </el-tab-pane>
        <el-tab-pane name="gold" label="金币兑换记录">
          <el-table :data="tableData" style="width: 100%">
            <el-table-column prop="userName" label="所属用户"/>
            <el-table-column prop="userName" label="兑换用户"/>
            <el-table-column prop="exchangeAmount" label="金币数量"/>
            <el-table-column prop="payTime" label="支付时间"/>
            <el-table-column prop="orderNo" label="兑换订单编号"/>
          </el-table>
        </el-tab-pane>
        <el-tab-pane name="integral" label="积分兑换记录">
          <el-table :data="tableData" style="width: 100%">
            <el-table-column prop="userName" label="所属用户"/>
            <el-table-column prop="userName" label="兑换用户"/>
            <el-table-column prop="exchangeAmount" label="积分额度"/>
            <el-table-column prop="payTime" label="支付时间"/>
            <el-table-column prop="orderNo" label="兑换订单编号"/>
          </el-table>
        </el-tab-pane>
        <pagination v-show="recordTotal>0" :total="recordTotal" :page.sync="queryRecordParams.pageNum"
                    :limit.sync="queryRecordParams.pageSize" @pagination="getRecordList"/>
      </el-tabs>
      <span slot="footer" class="dialog-footer">
        <el-button @click="open = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import {merchantList} from "@/api/pos/TUserInfo";
  import {listTTerminalTransInfo} from "@/api/pos/TTerminalTransInfo";
  import {exchangeRecordList} from "@/api/pos/TUserDefaultInfo";
  import {exchangeOrderList} from "@/api/pos/TOrderInfo";


  export default {
    name: "TUserInfo",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        recordTotal: 0,
        // 用户基础数据表格数据
        TUserInfoList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          userName: null,
          userPhone: null,
        },
        queryRecordParams: {
          pageNum: 1,
          pageSize: 10,
          userId: null,
          transType: null,
          transOperType: null,
          orderType: null,
          orderStatus: null,
        },
        // 表单参数
        form: {},
        // tab页名称
        activeName: "transfer",
        tableData: [],
      };
    },
    created() {
      this.getList();
    },
    methods: {
      // tab点击事件
      handleTabClick(tab, event) {
        this.queryRecordParams.pageNum = 1;
        this.queryRecordParams.pageSize = 10;
        this.getRecordList();
      },
      /** 查询用户基础数据列表 */
      getList() {
        this.loading = true;
        merchantList(this.queryParams).then(response => {
          this.TUserInfoList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          userName: null,
          userPhone: null,
          gradeCode: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 查询详情按钮操作 */
      handleDetail(row) {
        this.open = true;
        this.title = "详情";
        this.activeName = "transfer";
        this.queryRecordParams = {
          pageNum: 1,
          pageSize: 10,
          userId: row.userId,
        }
        this.terminalTransList()
      },
      // 分页按钮事件
      getRecordList() {
        this.queryRecordParams.pageNum = 1;
        this.queryRecordParams.pageNum = 10;
        switch (this.activeName) {
          case "transfer":
            this.queryRecordParams.transType = null;
            this.queryRecordParams.transOperType = null;
            this.terminalTransList();
            break;
          case "transaction":
            this.queryRecordParams.transType = "40";
            this.queryRecordParams.transOperType = "4002";
            this.tUserDefaultList();
            break
          case "reward":
            this.queryRecordParams.transType = "50";
            this.queryRecordParams.transOperType = "5002";
            this.tUserDefaultList();
            break;
          case "gold":
            this.queryRecordParams.orderType = "20";
            // this.queryRecordParams.orderStatus = "30";
            this.exchangeOrderList();
            break;
          case "integral":
            this.queryRecordParams.orderType = "21";
            // this.queryRecordParams.orderStatus = "30";
            this.exchangeOrderList();
            break;
        }
      },
      // 划拨记录
      terminalTransList() {
        console.log(this.queryRecordParams)
        listTTerminalTransInfo(this.queryRecordParams).then(res => {
          this.recordTotal = res.total;
          this.tableData = res.rows;
        })
      },
      tUserDefaultList() {
        exchangeRecordList(this.queryRecordParams).then(res => {
          this.recordTotal = res.total;
          this.tableData = res.rows;
        })
      },
      exchangeOrderList() {
        exchangeOrderList(this.queryRecordParams).then(res => {
          this.recordTotal = res.total;
          this.tableData = res.rows;
        })
      }
    }
  };
</script>
